package com.lxw.nonghe.controller;

import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.RolePermission;
import com.lxw.nonghe.service.RolePermissionService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rolePermission")
@RequiresPermission("role_perm:manage")
public class RolePermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/add")
    public Result<Boolean> addRolePermission(@RequestBody RolePermission rolePermission) {
        try {
            boolean result = rolePermissionService.save(rolePermission);
            if (result) {
                logUtils.recordLog("角色权限管理", "添加权限", "成功",
                        "角色ID:" + rolePermission.getRoleId() + ", 权限ID:" + rolePermission.getPermissionId());
                return Result.success("角色权限关联添加成功", true);
            } else {
                logUtils.recordLog("角色权限管理", "添加权限", "失败", "保存数据库失败");
                return Result.error("角色权限关联添加失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "添加权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> deleteRolePermission(@RequestParam Integer roleId, @RequestParam Integer permissionId) {
        try {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);

            boolean result = rolePermissionService.removeById(rolePermission);
            if (result) {
                logUtils.recordLog("角色权限管理", "删除权限", "成功",
                        "角色ID:" + roleId + ", 权限ID:" + permissionId);
                return Result.success("角色权限关联删除成功", true);
            } else {
                logUtils.recordLog("角色权限管理", "删除权限", "失败", "关联关系不存在");
                return Result.error("角色权限关联删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "删除权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateRolePermission(@RequestBody RolePermission rolePermission) {
        try {
            boolean result = rolePermissionService.updateById(rolePermission);
            if (result) {
                logUtils.recordLog("角色权限管理", "修改权限", "成功",
                        "角色ID:" + rolePermission.getRoleId() + ", 权限ID:" + rolePermission.getPermissionId());
                return Result.success("角色权限关联修改成功", true);
            } else {
                logUtils.recordLog("角色权限管理", "修改权限", "失败", "关联关系不存在");
                return Result.error("角色权限关联修改失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "修改权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<List<RolePermission>> listRolePermissions() {
        try {
            List<RolePermission> rolePermissions = rolePermissionService.list();
            /*logUtils.recordLog("角色权限管理", "查询全部关联", "成功",
                    "记录数:" + rolePermissions.size());*/
            return Result.success("查询成功", rolePermissions);
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "查询全部关联", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/listByRoleId")
    public Result<List<RolePermission>> listRolePermissionsByRoleId(@RequestParam Integer roleId) {
        try {
            List<RolePermission> rolePermissions = rolePermissionService.lambdaQuery()
                    .eq(RolePermission::getRoleId, roleId)
                    .list();
            /*logUtils.recordLog("角色权限管理", "按角色查询权限", "成功",
                    "角色ID:" + roleId + ", 权限数:" + rolePermissions.size());*/
            return Result.success("查询成功", rolePermissions);
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "按角色查询权限", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @PostMapping("/batchAdd")
    public Result<Boolean> batchAddRolePermission(
            @RequestParam Integer roleId,
            @RequestBody List<Integer> permissionIds) {
        try {
            boolean result = rolePermissionService.batchAdd(roleId, permissionIds);
            if (result) {
                logUtils.recordLog("角色权限管理", "批量添加权限", "成功",
                        "角色ID:" + roleId + ", 权限数:" + permissionIds.size());
                return Result.success("批量添加成功", true);
            } else {
                logUtils.recordLog("角色权限管理", "批量添加权限", "失败", "角色ID:" + roleId);
                return Result.error("批量添加失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "批量添加权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/batchDelete")
    public Result<Boolean> batchDeleteRolePermission(
            @RequestParam Integer roleId,
            @RequestBody List<Integer> permissionIds) {
        try {
            boolean result = rolePermissionService.batchDelete(roleId, permissionIds);
            if (result) {
                logUtils.recordLog("角色权限管理", "批量删除权限", "成功",
                        "角色ID:" + roleId + ", 权限数:" + permissionIds.size());
                return Result.success("批量删除成功", true);
            } else {
                logUtils.recordLog("角色权限管理", "批量删除权限", "失败", "角色ID:" + roleId);
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "批量删除权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/permissionsByRole")
    public Result<List<Integer>> getPermissionsByRole(@RequestParam Integer roleId) {
        try {
            List<Integer> permissionIds = rolePermissionService.getPermissionsByRole(roleId);
            /*logUtils.recordLog("角色权限管理", "获取角色权限ID", "成功",
                    "角色ID:" + roleId + ", 权限数:" + permissionIds.size());*/
            return Result.success("查询成功", permissionIds);
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "获取角色权限ID", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/rolesByPermission")
    public Result<List<Integer>> getRolesByPermission(@RequestParam Integer permissionId) {
        try {
            List<Integer> roleIds = rolePermissionService.getRolesByPermission(permissionId);
            /*logUtils.recordLog("角色权限管理", "获取权限角色ID", "成功",
                    "权限ID:" + permissionId + ", 角色数:" + roleIds.size());*/
            return Result.success("查询成功", roleIds);
        } catch (Exception e) {
            logUtils.recordLog("角色权限管理", "获取权限角色ID", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }
}
